package cn.cai.pethome.base.controller;

import cn.cai.pethome.base.util.AjaxResult;
import cn.cai.pethome.base.util.FastDfsApiOpr;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fastdfs")
public class FastdfsController {

    /**
     * 文件上传接口
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file){
        try {
            //1.获取上传的文件本来的名称
            String originalFilename = file.getOriginalFilename();
            //2.截取字符串，获取文件后缀名
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            String filePath = FastDfsApiOpr.upload(file.getBytes(), extName);
            return AjaxResult.success(filePath);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error("文件上传失败！" + e.getMessage());
        }
    }

}
